package com.tianma315.core.procedures.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;


import com.tianma315.core.common.base.CoreServiceImpl;
import com.tianma315.core.constant.StateEnum;
import com.tianma315.core.oss.service.FileService;
import com.tianma315.core.procedures.dao.ProcedureLinkDao;
import com.tianma315.core.procedures.domain.ProcedureLinkDO;
import com.tianma315.core.procedures.domain.ProceduresDO;
import com.tianma315.core.procedures.service.ProcedureLinkService;
import com.tianma315.core.procedures.service.ProceduresService;


import com.tianma315.core.procedures.vo.ProcedureLinkFileVO;

import com.tianma315.core.product.domain.ProductDO;
import com.tianma315.core.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;




import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 
 * <pre>
 * 环节
 * </pre>
 * <small> 2019-08-13 09:54:53 | Lgc</small>
 */
@Service
public class LinkServiceImpl extends CoreServiceImpl<ProcedureLinkDao, ProcedureLinkDO> implements ProcedureLinkService {
    @Autowired
    private FileService fileService;

    @Autowired
    private ProcedureLinkDao procedureLinkDao;

    @Autowired
    private ProductService productService;

    @Autowired
    private ProceduresService proceduresService;

    /**
     *  分页数据的查询
     * @param pageNumber 当前页数
     * @param pageSize 页的大小
     * @param procedureLinkDTO 实体类
     * @return 返回分页的数据
     */
    @Override
    public Page<ProcedureLinkFileVO> getProcedureLinkDOPage(Integer pageNumber, Integer pageSize, ProcedureLinkDO procedureLinkDTO) {
        Page<ProcedureLinkFileVO> page = new Page<>(pageNumber,pageSize);
        List<ProcedureLinkFileVO> list = procedureLinkDao.listProceduresVO(page,procedureLinkDTO.getProceduresId());
        page.setRecords(list);
        return page;
    }

    /**
     * 这个方法查询是否有相同条件的列表存在（目的为了查询出是否有相同的sort）
     * @param procedureLinkFileVO 实体类封装MultipartFile类型的属性
     * @return  返回符合条件的数据
     */
    @Override
    public List<ProcedureLinkDO> checkProcedureLinkSort(ProcedureLinkFileVO procedureLinkFileVO) {
        EntityWrapper<ProcedureLinkDO> entityWrapper = new EntityWrapper();
        //entityWrapper.eq("merchant_id",procedureLinkFileVO.getMerchantId());
        entityWrapper.eq("sort",procedureLinkFileVO.getSort());
        entityWrapper.eq("type",procedureLinkFileVO.getType());
        entityWrapper.eq("state", StateEnum.not_del);
        entityWrapper.eq("procedures_id",procedureLinkFileVO.getProceduresId());
        if (procedureLinkFileVO.getProcedureLinkId() != null){
            entityWrapper.ne("procedure_link_id",procedureLinkFileVO.getProcedureLinkId());
        }
        List<ProcedureLinkDO> list = selectList(entityWrapper);
        return list;
    }

    /**
     *  添加一条环节数据
     * @param procedureLinkFileVO 从实体类获得MultipartFile文件信息，将文件上传到七牛云返回url字符串数据保存数据库中
     * @return 返回插入数据是否成功
     */
    @Override
    public boolean addProcedureLink(ProcedureLinkFileVO procedureLinkFileVO) {
        String url=null;
        if (!procedureLinkFileVO.getIconFile().isEmpty()){
            try {
                url = fileService.upload(procedureLinkFileVO.getIconFile().getBytes(), procedureLinkFileVO.getIconFile().getOriginalFilename());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //修改父环节的isParent属性为1 代表有子环节。
        Integer parentId = procedureLinkFileVO.getParentId();
        if (parentId!=null){
            ProcedureLinkDO procedureLinkDO = new ProcedureLinkDO();
            procedureLinkDO.setProcedureLinkId(parentId);
            procedureLinkDO.setIsParent(1);
            updateById(procedureLinkDO);
        }
        procedureLinkFileVO.setCreateDate(Calendar.getInstance().getTime());
        procedureLinkFileVO.setState(StateEnum.not_del.getCode());
        procedureLinkFileVO.setIcon(url);
        Boolean result = insert(procedureLinkFileVO);
        return result;
    }

    /**
     *  删除逻辑就是修改数据state的状态码
     * @param procedureLinkId 根据Id去查询数据，然后修改数据更新
     * @return 返回是否成功
     */
    @Override
    public boolean deleteProcedureLink(Integer procedureLinkId) {
        ProcedureLinkDO procedureLinkDO = new ProcedureLinkDO();
        procedureLinkDO.setState(StateEnum.is_del.getCode());
        procedureLinkDO.setProcedureLinkId(procedureLinkId);
        Boolean result = updateById(procedureLinkDO);
        return result;
    }

    @Override
    public List<ProcedureLinkDO> getProcedureLinkByProductId(Integer productId) {
        ProductDO productDO = productService.getProductById(productId);
        ProceduresDO proceduresDO = proceduresService.getProceduresDOByProductId(productId);
        return null;
    }

    @Override
    public List<ProcedureLinkFileVO> getProcedureLinkFileVO(Integer proceduresId) {
        List<ProcedureLinkFileVO> list = procedureLinkDao.getProceduresVO(proceduresId);
        return list;
    }
}
